package cn.net.xyan.pay.core.service

import org.springframework.beans.factory.annotation.Autowired
import java.util.*
import kotlin.collections.HashMap
import kotlin.reflect.full.createInstance

/**
 * Created by zarra on 2017/4/12.
 */

enum class ButtonType {
    CLICK, URL
}

data class Button(val name: String, val type: ButtonType? = null) {
    var data: MutableMap<String, Any?> = HashMap()
    var content: String? = null
    val children: MutableList<Button> = LinkedList()

    fun child(block:Button.() -> Button){
        val b = Button::class.createInstance()
        b.block()
        children.add(b)
    }
}

class ButtonInfo{
    var name:String = ""
    var type:ButtonType? = null
    var content: String? = null
    private val children:MutableList<ButtonInfo> = LinkedList()

    fun build():Button{
        val b = Button(name,type)
        b.content = content
        children.forEach {
            b.children.add(it.build())
        }
        return b
    }
    fun child(block:ButtonInfo.() ->Unit):Unit{
        val buttonInfo = ButtonInfo()
        buttonInfo.block()
        children.add(buttonInfo)
    }
}

fun button(block: ButtonInfo.() -> Unit): Button {

    val buttonInfo = ButtonInfo()

    buttonInfo.block()

    return buttonInfo.build()
}

interface MenuService{
    fun deleteAll()
    fun addAll(buttons:List<Button>)
}

interface MenuManagerService{
    fun createMenu(buttons:List<Button>)
}

class DefaultMenuManager:MenuManagerService{

    @Autowired
    lateinit var menuServices:List<MenuService>

    override fun createMenu(buttons: List<Button>) {
        menuServices.forEach {
            it.deleteAll()
            it.addAll(buttons)
        }
    }

}